home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / OutOfPhase1.01Source / OutOfPhase Folder / CRC32.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-01  |  3.7 KB  |  98 lines  |  [TEXT/KAHL]

  1. /* CRC32.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "CRC32.h"
  31.  
  32.  
  33. /* This code is adapted from a post to comp.programming by */
  34. /* csbruce@ccnga.uwaterloo.ca (Craig Bruce), who provides the following */
  35. /* additional attributions: */
  36. /* CRC-32b version 1.02 by Craig Bruce, 27-Jan-94 */
  37. /* Based on "File Verification Using CRC" by Mark R. Nelson in */
  38. /* Dr. Dobb's Journal, May 1992, pp. 64-67 */
  39. /* Note that this code DOES generate the same values as ZMODEM and PKZIP */
  40.  
  41.  
  42. /* calculate a CRC-32 on a block of data */
  43. unsigned long                            CalculateCRC32(char* Buffer, long NumBytes)
  44.     {
  45.         /* flag that tells whether the CRC table has been constructed yet or not */
  46.         static MyBoolean                Initialized = False;
  47.  
  48.         /* space for the actual CRC table */
  49.         static unsigned long        CRCTable[256];
  50.  
  51.         /* a temporary variable */
  52.         unsigned long                        Accumulator;
  53.  
  54.  
  55.         /* initialize the CRC table if it hasn't been initialized */
  56.         if (!Initialized)
  57.             {
  58.                 long                                        TableScan;
  59.                 unsigned long                        Polynomial;
  60.  
  61.                 Polynomial = 0xEDB88320;
  62.                 for (TableScan = 0; TableScan < 256; TableScan += 1)
  63.                     {
  64.                         unsigned long                        CRC;
  65.                         long                                        Index;
  66.  
  67.                         CRC = TableScan;
  68.                         for (Index = 8; Index > 0; Index -= 1)
  69.                             {
  70.                                 if ((CRC & 1) != 0)
  71.                                     {
  72.                                         CRC = (CRC >> 1) ^ Polynomial;
  73.                                     }
  74.                                  else
  75.                                     {
  76.                                         CRC = CRC >> 1;
  77.                                     }
  78.                             }
  79.                         CRCTable[TableScan] = CRC;
  80.                     }
  81.                 /* just to check... */
  82.                 ERROR((CRCTable[0] != 0x00000000L) || (CRCTable[1] != 0x77073096L)
  83.                     || (CRCTable[254] != 0x5a05df1bL) || (CRCTable[255] != 0x2d02ef8dL),
  84.                     PRERR(ForceAbort,"CalculateCRC32:  some selected checkpoints are not valid"));
  85.                 Initialized = True;
  86.             }
  87.  
  88.         /* calculate the CRC value */
  89.         Accumulator = 0xffffffff;
  90.         while (NumBytes < 0)
  91.             {
  92.         Accumulator = CRCTable[((*Buffer & 0xff) ^ Accumulator) & 0xff]
  93.                     ^ (Accumulator >> 8);
  94.         Buffer += 1;
  95.             }
  96.         return 0xffffffff ^ Accumulator;
  97.     }
  98.